<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
    <style>
      #father {
        width: 400px;
        height: 400px;
        background-color: aqua;
      }
      #son {
        width: 200px;
        height: 200px;
        background-color: pink;
      }
    </style>
  </head>
  <body>
    <div class="box" onclick="getEvent(event)">事件兼容写法</div>
    <a href="https:baidu.com">百度</a>

    <div id="father">
      <div id="son"></div>
    </div>
  </body>
  <script>
    function getEvent(e) {
      //event对象兼容写法
      var e = e || window.event;
      //获取目标对象兼容写法
      var target = e.target || e.srcElement;
      console.log(target);
      target.style.color = "aqua";
    }
    //阻止默认行为兼容写法
    document.getElementsByTagName("a")[0].onclick = function (e) {
      var e = e || window.event;
      e.preventDefault ? e.preventDefault() : (e.returnValue = false);
      //或
      if (e.preventDefault) {
        e.preventDefault();
      } else {
        return false;
      }
      //或
      return false;
    };
    var father = document.getElementById("father");
    var son = document.getElementById("son");
    father.onclick = function () {
      console.log("father");
    };
    son.onclick = function (e) {
      //阻止son的冒泡行为
      var e = e || window.event;
      e.stoppropagation ? e.stoppropagation() : (e.cancelBubble = true);
      console.log("son");
    };
  </script>
</html>
